define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coursepaper/index' + location.search,
                    add_url: 'coursepaper/add',
                    edit_url: 'coursepaper/edit',
                    del_url: 'coursepaper/del',
                    multi_url: 'coursepaper/multi',
                    import_url: 'coursepaper/import',
                    table: 'course_paper'
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                // 表单
                var form = $("form", table.$commonsearch);

                // 设置绑定课程
                $("input[name='course.id']", form).addClass("selectpage").data("source", "course/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);

                // 设置绑定分类
                $("input[name='category_id']", form).addClass("selectpage").data("source", "coursequestioncategory/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10).data("params", function (obj) {
                    return {custom: {course_id: $("input[name='course.id']").val()}};
                });

                // 注册事件
                Form.events.selectpage(form);
            });



            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('试卷编号')},
                        {field: 'name', title: __('试卷名称'), operate: 'LIKE'},
                        {field: 'question_count', title: __('题目数量'), operate: 'LIKE',searchable: false},
                        {field: 'course.id', title: __('关联课程'),visible:false},
                        {field: 'course.name', title: __('关联课程'), searchable: false},
                        {field: 'category.name', title: __('关联分类'),searchable: false},
                        {field: 'category_id', title: __('关联分类'), visible:false},
                        {
                            field: 'is_need_vip', 
                            title: __('会员试卷'), 
                            formatter: Table.api.formatter.normal,
                            searchList: {1: __('是'), 0: __('否')}
                        },
                        {field: 'sort', title: __('排序值')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            buttons:[
                                {
                                    name: 'detail',
                                    text: '查看题目',
                                    title: '查看题目',
                                    icon: 'fa fa-question-circle',
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    url: function (row) {
                                        return 'question/index/extend_id/'+row.id;
                                    }
                                }
                            ],
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 监听导入题库事件
            $('.btn-add-question').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'coursepaper/importQuestion/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导入题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听导出题库事件
            $('.btn-export-question').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'coursepaper/exportQuestion/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导出题目'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });


            // 监听导出电子版事件
            $('.btn-export-word').click(function (){
                var selectIds = Table.api.selectedids(table);
                var url = 'coursepaper/exportWord/ids/'+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('导出电子版'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 监听清空题目事件
            $('.btn-clear-question').click(function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定清空选中试卷的题目数据?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true, btn: [__('OK'), __('Cancel')]},
                    function (index) {
                        Table.api.multi("clearquestion", ids, table, that);
                        Layer.close(index);
                    }
                );
            });
            
            // 监听批量标记为会员试卷事件
            $('.btn-batch-setmemberpaper').click(function (){
                var selectIds = Table.api.selectedids(table);
                if (selectIds.length === 0) {
                    Layer.alert(__('请选择需要操作的试卷'));
                    return false;
                }
                var url = 'coursepaper/batchsetmemberpaper?ids='+selectIds.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('批量标记为会员试卷'), $(this).data() || {}, {
                    success: function () {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-category_id").data("params", function (obj) {
                return {custom: {course_id: $("#c-course_id").val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        importquestion:function (){
            Controller.api.bindevent();
        },
        exportquestion:function (){
            Controller.api.bindevent();
            Form.api.bindevent("form[role=form]", function(data, ret){
                window.location.href = data;
                return true;
            });
        },
        exportword:function (){
            Controller.api.bindevent();
            Form.api.bindevent("form[role=form]", function(data, ret){
                console.log(data.data.fileUrl);
                window.location.href = data.data.fileUrl;
                return false;
            });
        },
        batchsetmemberpaper: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
